<template>
    <el-dialog title="新 增" :visible.sync="myResult" :before-close="handleClose">
        <el-form ref="form" :model="form" label-width="135px" :rules="rules">
        <el-form-item label="名称" prop="title">
            <el-input v-model="form.title"></el-input>
        </el-form-item>
        <el-form-item label="请求方式" prop="method">
             <el-input v-model="form.method"></el-input>
        </el-form-item>
         <el-form-item label="路径" prop="path">
             <el-input v-model="form.path"></el-input>
        </el-form-item>
        <el-form-item label="权限" prop="permission">
             <el-input v-model="form.permission"></el-input>
        </el-form-item>
        <el-form-item label="类型"  prop="type">
             <el-radio-group v-model="form.type" size="medium">
                <el-radio border label="1" >菜单</el-radio>
                <el-radio border label="2" >功能</el-radio>
            </el-radio-group>
        </el-form-item>
        <el-form-item label="上级"  prop="parentId">
            <el-select v-model="form.parentId" placeholder="请选择上级菜单">
                <el-option value=-1 label="根"></el-option>
                <el-option v-for=" (item,key) of parentIds" :key="key" :value="item.id" :label="item.title"></el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="排序"
             prop="sort">
            <el-input type="sort" v-model.number="form.sort" autocomplete="off"></el-input>
        </el-form-item>
         <el-form-item label="图标" prop="icon">
            <d2-icon-select v-model="form.icon" :user-input="true"/>
        </el-form-item>
        <el-form-item label="备注"  prop="description">
            <el-input type="textarea" v-model="form.description"></el-input>
        </el-form-item>
        <el-form-item>
            <el-button type="primary" @click="onSubmit('form')">创 建</el-button>
            <el-button @click="resetForm('form')">重 置</el-button>
            <el-button @click="myResult=false">取 消</el-button>
        </el-form-item>
        </el-form>
    </el-dialog>
</template>
<script>
import { LoadPermissionType,SavePermission } from '@api/permission/permission'
import index from '@/components/d2-icon/index.vue'
  export default {
    data() {
      return {
          components:{

          },
          myResult: this.result,
          icon: '',
        form: {
          title: '',
          path: '',
          method: '',
          type: '',
          permission: '',
          parentId:'',
          sort: '',
          description:'',
          icon:'',
          scope: 1
        },
        parentIds:[
        ],
        rules: {
            title: [
            { required: true, message: '请输入Title', trigger: 'blur' },
            { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
          ],
          path: [
            { required: true, message: '请输入path', trigger: 'blur' },
            { min: 3, max: 50, message: '长度在 3 到 50 个字符', trigger: 'blur' }
          ],
          type: [
            { required: true, message: '请选择Type', trigger: 'change' }
          ],
          parentId: [
            { required: true, message: '请选择ParentId', trigger: 'change' }
          ],
          sort:[
                { required: true, message: '请输入sort'},
               { type: 'number', message: '必须为数字值'}
            ]
        //     ,
        //   icon: [
        //     { required: true, message: '请输入icon', trigger: 'blur' },
        //     { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
        //   ],
        }
      }
    },
    /**双向绑定 start */
    props: ["result"],
    watch: {
        result(val) {
            this.myResult = val;//②监听外部对props属性result的变更，并同步到组件内的data属性myResult中
        },
        myResult(val){
            //xxcanghai 小小沧海 博客园
             this.$emit("on-result-change",val);//③组件内对myResult变更后向外部发送事件通知
        }
    },
  
    methods: {
        change() {
            this.myResult = !this.myResult;
            this.$refs['form'].resetFields();
        },
          /**双向绑定 end */

        onSubmit(formName) {
            this.$refs[formName].validate((valid) => {
            if (valid) {
                this.loading = true
                SavePermission(this.form).then(res=>{
                    this.$message({
                    message: '创建成功',
                    type: 'success'
                    });
                     this.$emit("on-loadlist-change");//③组件内对myResult变更后向外部发送事件通知
                     this.loading = false
                     this.myResult=false
                }).catch(err=>{
                    console.log('err', err)
                    this.loading = false
                })
               
            } else {
                console.log('error submit!!');
                return false;
            }
            });
        },
        resetForm(formName) {
          this.$refs[formName].resetFields();
         },
        handleClose(done){
            this.$confirm('确定关闭吗').then(() => {
                // function(done)，done 用于关闭 Dialog
                done();
            }).catch(() => {
               // console.log("点击确定时触发");
            });
        },
        fetchData () {
            this.loading = true
            LoadPermissionType({type:1}).then(res => {
                this.parentIds = res.data
                this.loading = false
            }).catch(err => {
                console.log('err', err)
                this.loading = false
            })
        }
    },
    created(){
        //console.log("created");
    },
    mounted(){
        //console.log("mounted");

        this.fetchData()
    },
    computed:{
       
    }
  }
</script>
<style lang="scss" scoped>
.title-group {
  margin-top: 20px;
  margin-bottom: 10px;
  &:first-child {
    margin-top: 0px;
  }
  .title {
    margin: 0px;
  }
  .sub-title {
    margin: 0px;
    color: $color-text-sub;
    font-size: 10px;
  }
}
</style>